unit uPageManager;

interface
uses
  GlScene, GlMaterial,
  uBasePage, uGLBasePage;
type
//------------------------------------------------------------------------------
  TPageManager = class
  protected
    fParent: TGLBaseSceneObject;
    fMaterialLibrary: TGlMaterialLibrary;
    fRegistered: TPageList;
    fStack: TPageList;
  protected
    Procedure OnPageShowComplete(aPage: TBasePage);
    Procedure OnPageHideComplete(aPage: TBasePage);
  public
    Function RegisterPage(aPageClass: TGLBasePageClass): TGlBasePage;
    Function Push(aPageClass: TBasePageClass): TBasePage;
    Function ForcedPush(aPageClass: TBasePageClass): TBasePage;
    Function Pop: TBasePage;
    Function PopPage(aPageClass: TBasePageClass): TBasePage;

    Function GetPageByClass(aPageClass: TGLBasePageClass): TGlBasePage;
    Procedure Update(aDeltaTime: Single);
    Constructor Create(aGlParent: TGLBaseSceneObject; aMaterialLibrary: TGlMaterialLibrary);
  end;
//------------------------------------------------------------------------------
implementation

uses Classes;
//------------------------------------------------------------------------------
Procedure TPageManager.OnPageShowComplete(aPage: TBasePage);
begin
end;
//------------------------------------------------------------------------------
Procedure TPageManager.OnPageHideComplete(aPage: TBasePage);
begin
  fStack.DeletePage(aPage);
  // fStack.SetParent(nil); 
end;
//------------------------------------------------------------------------------
Function TPageManager.RegisterPage(aPageClass: TGLBasePageClass): TGlBasePage;
begin
  result := TGlBasePage(fRegistered.AddPageByClass(aPageClass));
  result.InitGLPage(fParent, fMaterialLibrary);
  result.OnHideComplete := OnPageHideComplete;
  result.OnShowComplete := OnPageShowComplete;
end;
//------------------------------------------------------------------------------
Function TPageManager.Push(aPageClass: TBasePageClass): TBasePage;
begin
  result := fRegistered.GetPageByClass(aPageClass);
  fStack.Add(result);
  result.Show;
  //result.SetParent(fDummy);
end;
//------------------------------------------------------------------------------
Function TPageManager.ForcedPush(aPageClass: TBasePageClass): TBasePage;
begin
  result := Push(aPageClass);
  result.ForcedShow;
end;
//------------------------------------------------------------------------------
Function TPageManager.Pop: TBasePage;
begin
  result := fStack.GetPageByIndex(fStack.Count - 1);
  if result <> nil then
    result.Hide;
end;
//------------------------------------------------------------------------------
Function TPageManager.PopPage(aPageClass: TBasePageClass): TBasePage;
begin
  result := fStack.GetPageByClass(aPageClass);
  result.Hide;  
end;
//------------------------------------------------------------------------------
Function TPageManager.GetPageByClass(aPageClass: TGLBasePageClass): TGlBasePage;
begin
  result := TGlBasePage(fRegistered.GetPageByClass(aPageClass));
end;
//------------------------------------------------------------------------------
Procedure TPageManager.Update(aDeltaTime: Single);
var
  i: integer;
begin
  for i := 0 to fStack.Count - 1 do
    fStack.GetPageByIndex(i).Update(aDeltaTime);
end;
//------------------------------------------------------------------------------
Constructor TPageManager.Create(aGlParent: TGLBaseSceneObject; aMaterialLibrary: TGlMaterialLibrary);
begin
  fParent := aGlParent;
  fMaterialLibrary := aMaterialLibrary;
  fRegistered := TPageList.Create;
  fStack := TPageList.Create;

  fStack.GetPageByClass(TBasePage);
end;
//------------------------------------------------------------------------------
end.
